home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / mail / delivery / deliver.tz / deliver / context.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-07  |  2.6 KB  |  148 lines

  1. /* $Header: context.c,v 2.2 90/02/23 16:35:52 chip Exp $
  2.  *
  3.  * User context manager.
  4.  * This module exists for efficiency reasons; I could just call getpwnam()
  5.  * every time I need context info.
  6.  *
  7.  * $Log:    context.c,v $
  8.  * Revision 2.2  90/02/23  16:35:52  chip
  9.  * \Fix problems determining legality of user references.
  10.  * 
  11.  * Revision 2.1  89/06/09  12:25:13  network
  12.  * Update RCS revisions.
  13.  * 
  14.  * Revision 1.5  89/06/09  12:23:39  network
  15.  * Baseline for 2.0 release.
  16.  * 
  17.  */
  18.  
  19. #include "deliver.h"
  20. #include <pwd.h>
  21. #include <grp.h>
  22.  
  23. extern  struct passwd   *getpwnam();
  24. extern  struct passwd   *getpwuid();
  25. extern  struct group    *getgrnam();
  26. extern  struct group    *getgrgid();
  27.  
  28. /*
  29.  * Local functions.
  30.  */
  31.  
  32. static  CONTEXT *new_context();
  33.  
  34. /*
  35.  * Local data.
  36.  */
  37.  
  38. static  CONTEXT *ctlist;    /* Chain of CONTEXT structures.        */
  39.  
  40. /*----------------------------------------------------------------------
  41.  * Look up a context by user name.
  42.  */
  43.  
  44. CONTEXT *
  45. name_context(name)
  46. char    *name;
  47. {
  48.     struct passwd *pw;
  49.     CONTEXT *ct;
  50.  
  51.     for (ct = ctlist; ct; ct = ct->ct_next)
  52.     {
  53.         if (strcmp(ct->ct_name, name) == 0)
  54.             return ct;
  55.     }
  56.  
  57.     if ((pw = getpwnam(name)) == NULL)
  58.         return NULL;
  59.  
  60.     return new_context(pw);
  61. }
  62.  
  63. /*----------------------------------------------------------------------
  64.  * Look up a context by user ID.
  65.  */
  66.  
  67. CONTEXT *
  68. uid_context(uid)
  69. int     uid;
  70. {
  71.     struct passwd *pw;
  72.     CONTEXT *ct;
  73.  
  74.     for (ct = ctlist; ct; ct = ct->ct_next)
  75.     {
  76.         if (ct->ct_uid == uid)
  77.             return ct;
  78.     }
  79.  
  80.     if ((pw = getpwuid(uid)) == NULL)
  81.         return NULL;
  82.  
  83.     return new_context(pw);
  84. }
  85.  
  86. /*----------------------------------------------------------------------
  87.  * Local function -- create a new context structure and return
  88.  * its address.
  89.  */
  90.  
  91. static CONTEXT *
  92. new_context(pw)
  93. struct passwd *pw;
  94. {
  95.     CONTEXT *ct;
  96.  
  97.     ct = (CONTEXT *) zalloc(sizeof(CONTEXT));
  98.     ct->ct_uid = pw->pw_uid;
  99.     ct->ct_gid = pw->pw_gid;
  100.     ct->ct_name = copystr(pw->pw_name);
  101.     ct->ct_home = copystr(pw->pw_dir);
  102.  
  103.     ct->ct_next = ctlist;
  104.     ctlist = ct;
  105.  
  106.     return ct;
  107. }
  108.  
  109. /*----------------------------------------------------------------------
  110.  * Report whether is is possible or not to go from the given
  111.  * effective uid/real uid/real gid to the given context.
  112.  */
  113.  
  114. int
  115. ok_context(euid, ruid, rgid, ct)
  116. int    euid, ruid, rgid;
  117. CONTEXT *ct;
  118. {
  119.     if (!ct)
  120.         return FALSE;
  121.  
  122.     if (euid == 0
  123.      || ((ruid == ct->ct_uid) && (rgid == ct->ct_gid)))
  124.         return TRUE;
  125.     else
  126.         return FALSE;
  127. }
  128.  
  129. /*----------------------------------------------------------------------
  130.  * Look up a group ID by name.
  131.  */
  132.  
  133. #ifdef MBX_GROUP
  134.  
  135. int
  136. group_id(name)
  137. char    *name;
  138. {
  139.     struct group *grp;
  140.  
  141.     if ((grp = getgrnam(name)) == NULL)
  142.         return -1;
  143.  
  144.     return grp->gr_gid;
  145. }
  146.  
  147. #endif /* MBX_GROUP */
  148.